#!/bin/bash

# 定义变量
REMOTE_USER="root"       # 远程Linux服务器的用户名
REMOTE_HOST="8.149.128.109"     # 远程Linux服务器的主机名或IP地址
REMOTE_PORT="22"
REMOTE_DIR="/u01/front"  # 远程Linux服务器的目标目录
JAR_DIR="."  # 本地文件或目录的路径
PASSWORD="Qdxiaoquankeji123!"
# 查找最新的.jar文件
LATEST_JAR=$(find "$JAR_DIR" -type f -name "dist-prod.zip" -print0 | xargs -0 ls -t | head -n 1)

if [ -n "$LATEST_JAR" ]; then
    JAR_NAME=$(basename "$LATEST_JAR")
    echo "Uploading latest JAR: $JAR_NAME"
    echo "Uploading latest JAR: $LATEST_JAR"
    timeCurrent=$(date "+%Y%m%d%H%M%S")
    backupName=$JAR_NAME-$timeCurrent
    backup_cmd="cp $JAR_NAME $backupName"
    echo "当前时间 $timeCurrent"
    bac_cmd="cd $REMOTE_DIR && $backup_cmd"
    sshpass -p "$PASSWORD" ssh "$REMOTE_USER@$REMOTE_HOST" "$bac_cmd"
    echo "备份 $backupName 成功"
    # 使用scp命令上传文件
    sshpass -p "$PASSWORD" scp  -P $REMOTE_PORT  "$LATEST_JAR" $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR
    echo "上传成功 $LATEST_JAR"
   # START_CMD="cd $REMOTE_DIR && sh start.sh"
   # sshpass -p "$PASSWORD" ssh "$REMOTE_USER@$REMOTE_HOST" "$START_CMD"
    # 检查scp命令执行状态
  #  if [ $? -eq 0 ]; then
   #     echo "文件上传并重启成功"
   # else
   #     echo "文件上传失败"
   # fi
else
    echo "No JAR files found to upload."
fi
